<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'任务计划', styles:'bootstrap,font-awesome,animate,style,bootstrap-table'}){}%>
    </head>

  <body>


      <div class="wrapper wrapper-content">
              <!-- START 内容部分 -->
              <div class="row">

                  <div class="col-sm-11" >

                    <!-- START 操作按钮 -->
                    <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
                      <button type="button" class="add-btn btn btn-sm btn-success"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 新增</button>
                      <button type="button" class="del-btn btn btn-sm btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button>
                      <button type="button" class="show-planingjob-btn btn btn-sm btn-warning"><i class="fa fa-list" ></i> 查看计划中任务</button>
                      <button type="button" class="show-runingjob-btn btn btn-sm btn-info"><i class="fa fa-play"></i> 查看运行中任务</button>
                    </div>
                    <!-- END 操作按钮 -->

                    <!-- START 数据表格 -->
                    <table id="powerscheduletable"
                               data-toolbar="#toolbar"
                               data-side-pagination="server"
                               data-pagination="true"
                               data-resizable="true"
                               data-show-columns="true"
                               data-side-pagination="server"
                               data-url="${ctxPath}/power/schedule/findall.do"
                               data-page-size="15">
                    </table>
                    <!-- END 数据表格 -->
                  </div>
              </div>
    </div>
    <%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,bootstrap-table-zh-CN,layer'}){}%>

    <script>
    var $powerscheduletable = $('#powerscheduletable'),selections=[],current_pid = 0;
    $(function(){

        //START 表格配置
        $powerscheduletable.bootstrapTable({
            height: getHeight(),
            columns: [
                {
                    field: 'state',
                    checkbox: true,
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '任务编号',
                    field: 'id',
                    align: 'left',
                    valign: 'middle',
                    visible: false
                },
                {
                    title: '任务名称',
                    field: 'jobName',
                    align: 'left',
                    valign: 'middle'
                },
                {
                    title: '任务分组',
                    field: 'jobGroup',
                    align: 'left',
                    valign: 'middle'
                },
                {
                    title: '执行时间表达式',
                    field: 'jobCron',
                    align: 'left',
                    valign: 'middle'
                },
                {
                    title: '任务类完整路径',
                    field: 'jobClassPath',
                    align: 'left',
                    valign: 'middle'
                },
                {
                    title: '任务状态',
                    field: 'jobStatus',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(v){
                        var val = '';
                        switch (v){
                            case 'stop':
                                val = '<i class="fa fa-stop text-danger"></i>';
                                break;
                            case 'start':
                                val = '<i class="fa fa-play text-info"></i>';
                                break;
                            case 'pause':
                                val = '<i class="fa fa-pause text-warning"></i>';
                                break;
                            default:
                                break;
                        }
                        return val;
                    }
                },
                {
                    title: '任务描述',
                    field: 'jobDescription',
                    align: 'left',
                    valign: 'middle',
                    sortable: true
                },
                {
                    field: 'operate',
                    title: '操作',
                    width: 200,
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function(){

                //修改任务计划
                $('.edit-powerschedule-btn').on('click',function(){
                    var powerscheduleid = $(this).attr('data-val');
                    layer.open({
                        title: '修改任务计划',
                        type: 2,
                        area: ['780px', '430px'],
                        content: ['${ctxPath}/power/schedule/editpage.do?id='+powerscheduleid, 'no']
                    });
                });

                //删除任务计划
                $('.del-powerschedule-btn').on('click',function(){

                    var delid = $(this).attr('data-val'),delname =$(this).attr('data-val-name');
                    layer.confirm('确定要删除任务？<br>[<strong>'+delname+'</strong>]', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        $.post('${ctxPath}/power/schedule/delete.do',{ids : delid},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    $powerscheduletable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,time: 1500});
                            }
                        });
                    });

                });


                //启动任务计划
                $('.start-powerschedule-btn').on('click',function(){
                    $.post('${ctxPath}/power/schedule/start.do',{id:$(this).attr('data-val')},function(data){
                        if( data.ok ){
                            layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                //刷新页面
                                $powerscheduletable.bootstrapTable('refresh');
                            });
                        }else{
                            layer.msg(data.msg,{icon: 0,time: 1500});
                        }
                    });
                });

                //停止任务计划
                $('.stop-powerschedule-btn').on('click',function(){
                    $.post('${ctxPath}/power/schedule/stop.do',{id:$(this).attr('data-val')},function(data){
                        if( data.ok ){
                            layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                //刷新页面
                                $powerscheduletable.bootstrapTable('refresh');
                            });
                        }else{
                            layer.msg(data.msg,{icon: 0,time: 1500});
                        }
                    });
                });

                //暂停任务计划
                $('.pause-powerschedule-btn').on('click',function(){
                    $.post('${ctxPath}/power/schedule/pause.do',{id:$(this).attr('data-val')},function(data){
                        if( data.ok ){
                            layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                //刷新页面
                                $powerscheduletable.bootstrapTable('refresh');
                            });
                        }else{
                            layer.msg(data.msg,{icon: 0,time: 1500});
                        }
                    });
                });

            }
        });
        //END 表格配置

        //新增任务计划
        $('.add-btn').on('click',function(){//新增
            layer.open({
                title: '新增任务计划',
                type: 2,
                area: ['790px', '500px'],
                skin: 'layer-ext-espresso',
                content: ['${ctxPath}/power/schedule/addpage.do', 'no']
            });
        });

        //删除任务计划
        $('.del-btn').on('click',function(){
            var selDatas = $powerscheduletable.bootstrapTable('getSelections');
            if( selDatas.length == 0 ){
                layer.msg('请选择要删除的任务计划',{icon: 0,shade: [0.8, '#393D49'],time: 1500});
            }else{
                var ids = [],names = [];
                $.each(selDatas,function(i,d){
                    ids.push(d.id);
                    names.push(d.jobName);
                });
                ids = ids.join(',');
                names = names.join(',');

                layer.confirm('确定要删除这些任务？<br>[<strong>'+names+'</strong>]', {
                    btn: ['确定','取消'],title:'提示'
                }, function(){
                    $.post('${ctxPath}/power/schedule/delete.do',{ids: ids},function(data){
                        if( data.ok ){
                            layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                //刷新页面
                                $powerscheduletable.bootstrapTable('refresh');
                            });
                        }else{
                            layer.msg(data.msg,{icon: 0,time: 1500});
                        }
                    });
                });

            }
        });
        
        //表格高度自适应
        $(window).resize(function () {
            $powerscheduletable.bootstrapTable('resetView', {
                height: getHeight()
            });
        });
    });

    //操作菜单
    function optFormatter(value,row){
        var content = '<button class="edit-powerschedule-btn btn btn-xs btn-info" data-val="'+row.id+'" title="修改"><i class="fa fa-edit"></i>  </button>';
        content += '&nbsp;<button class="del-powerschedule-btn btn btn-xs btn-danger" data-val="'+row.id+'" data-val-name="'+row.jobName+'" title="删除"><i class="fa fa-trash-o"></i>  </button>&nbsp;|';
        content += '&nbsp;<button class="start-powerschedule-btn btn btn-xs btn-info" data-val="'+row.id+'" title="启动"><i class="fa fa-play"></i>  </button>';
        content += '&nbsp;<button class="pause-powerschedule-btn btn btn-xs btn-info" data-val="'+row.id+'" title="暂停"><i class="fa fa-pause"></i>  </button>';
        content += '&nbsp;<button class="stop-powerschedule-btn btn btn-xs btn-danger" data-val="'+row.id+'" title="停止"><i class="fa fa-stop"></i>  </button>';
        return content;
    }
    
    //表格高度计算
    function getHeight() {
        return $(window).height() - $('.btn-group').outerHeight(true) - $('.breadcrumb').outerHeight(true) - 100;
    }
    </script>
  </body>
</html>
